package com.aiapphub.BaseCode.common;

import com.aiapphub.BaseCode.constants.AppHttpCodeEnum;
import com.fasterxml.jackson.annotation.JsonInclude;
import java.io.Serializable;

@JsonInclude(JsonInclude.Include.NON_NULL)
public class ResponseResult<T> implements Serializable {
    private Integer code; // 状态码
    private String msg; // 响应消息
    private T data; // 响应数据

    // 无参构造函数，默认为成功响应
    public ResponseResult() {
        this.code = AppHttpCodeEnum.SUCCESS.getCode();
        this.msg = AppHttpCodeEnum.SUCCESS.getMsg();
    }

    // 构造函数，传入状态码和响应数据
    public ResponseResult(Integer code, T data) {
        this.code = code;
        this.data = data;
    }

    // 构造函数，传入状态码、响应消息和响应数据
    public ResponseResult(Integer code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    // 构造函数，传入状态码和响应消息
    public ResponseResult(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    // 返回错误响应结果
    public static ResponseResult errorResult(int code, String msg) {
        ResponseResult result = new ResponseResult();
        return result.error(code, msg);
    }

    // 返回成功响应结果
    public static ResponseResult okResult() {
        ResponseResult result = new ResponseResult();
        return result;
    }

    // 返回成功响应结果，传入状态码和响应消息
    public static ResponseResult okResult(int code, String msg) {
        ResponseResult result = new ResponseResult();
        return result.ok(code, null, msg);
    }

    // 返回成功响应结果，传入响应数据
    public static ResponseResult okResult(Object data) {
        ResponseResult result = setAppHttpCodeEnum(AppHttpCodeEnum.SUCCESS, AppHttpCodeEnum.SUCCESS.getMsg());
        if(data!=null) {
            result.setData(data);
        }
        return result;
    }

    // 返回错误响应结果，传入AppHttpCodeEnum枚举类型
    public static ResponseResult errorResult(AppHttpCodeEnum enums){
        return setAppHttpCodeEnum(enums,enums.getMsg());
    }

    // 返回错误响应结果，传入AppHttpCodeEnum枚举类型和响应消息
    public static ResponseResult errorResult(AppHttpCodeEnum enums, String msg){
        return setAppHttpCodeEnum(enums,msg);
    }

    // 返回成功响应结果，传入AppHttpCodeEnum枚举类型
    public static ResponseResult setAppHttpCodeEnum(AppHttpCodeEnum enums){
        return okResult(enums.getCode(),enums.getMsg());
    }

    // 私有方法，传入AppHttpCodeEnum枚举类型和响应消息，返回成功响应结果
    private static ResponseResult setAppHttpCodeEnum(AppHttpCodeEnum enums, String msg){
        return okResult(enums.getCode(),msg);
    }

    // 返回错误响应结果，传入状态码和响应消息
    public ResponseResult<?> error(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
        return this;
    }

    // 返回成功响应结果，传入状态码和响应数据
    public ResponseResult<?> ok(Integer code, T data) {
        this.code = code;
        this.data = data;
        return this;
    }

    // 返回成功响应结果，传入状态码、响应数据和响应消息
    public ResponseResult<?> ok(Integer code, T data, String msg) {
        this.code = code;
        this.data = data;
        this.msg = msg;
        return this;
    }

    // 返回成功响应结果，传入响应数据
    public ResponseResult<?> ok(T data) {
        this.data = data;
        return this;
    }

    // getter和setter方法
    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
